<?
/* SECURITY (don't touch) */
defined('_INCL')||(header('HTTP/1.1 403 Forbidden')&die('403.14 - Access denied.'));
/**************************/

$GlyphCodings = array(//6bit encoding inspired by braile-dots :)
	'0'=>'000111',
	'1'=>'001000',
	'2'=>'001010',
	'3'=>'001100',
	'4'=>'001101',
	'5'=>'001001',
	'6'=>'001110',
	'7'=>'001111',
	'8'=>'001011',
	'9'=>'000110',
	
	'A'=>'010000',
	'B'=>'101000',
	'C'=>'110000',
	'D'=>'110100',
	'E'=>'100100',
	'F'=>'111000',
	'G'=>'111100',
	'H'=>'101100',
	'I'=>'011000',
	'J'=>'011100',
	'K'=>'100010',
	'L'=>'101010',
	'M'=>'110010',
	'N'=>'110110',
	'O'=>'100110',
	'P'=>'111010',
	'Q'=>'111110',
	'R'=>'101110',
	'S'=>'011010',
	'T'=>'011110',
	'U'=>'100011',
	'V'=>'101011',
	'W'=>'011101',
	'X'=>'110011',
	'Y'=>'110111',
	'Z'=>'100111',
);

$WorkDirName = 'QRimages_'.hash('adler32', _CRYPT2._CRYPT1).DIRECTORY_SEPARATOR;
$WorkDir = __DIR__.DIRECTORY_SEPARATOR.$WorkDirName;

if( !is_dir($WorkDir) ){
	mkdir($WorkDir);
	file_put_contents($WorkDir.'index.php','Closed Area.',LOCK_EX);
}


$ImagePrefix = 'QR_';
$FilePrefix = 'Serie_';
$SeriesGlyphs = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','R','S','T','U','V','X','Y','Z');
$ProdSeries = array('Free'=>array(),'Existing'=>array());






//----------------------------------------------
function codeData($Content,$Secret='TESTING',$Direction){
	$Content = str_split($Content);

	if($Direction === 'encode'){
		$String = ''; $i=0;
		$Content = array_reverse($Content);
		foreach($Content as $Glyph){
			$String .= $Glyph.substr($Secret,$i,1);
			$i++;
		}
		return $String;
		
	} else {//Decoding
		$Secret = ''; $Number = '';
		for($i=0; $i<count($Content); $i++){
			$Number .= $Content[$i];
			$i++;
			$Secret .= $Content[$i];
		}
		return strrev($Number).'|'.$Secret;
	}
}


//----------------------------------------------
function trackSeries(){
	global $WorkDirName,$WorkDir,$FilePrefix,$SeriesGlyphs,$ProdSeries,$ImagePrefix;
	
	foreach($SeriesGlyphs as $A1){//Construct all possible Alphas
		foreach($SeriesGlyphs as $A2){
			$ProdSeries['Free'][$A1.$A2] = NULL;
		}
	}
	
	if( $H=opendir($WorkDir) ){//Upgrade array with existing files
		while( ($Item=readdir($H)) !== false ){
			if( strpos($Item,$FilePrefix) === 0 && substr($Item,-4) === '.zip' ){
				$ImgDir = substr($Item,0,-4);
				
			//Check for checksum and existence of directory etc. for sanity/security
				$RecordedChecksum = explode('|',$Item);
				$RecordedChecksum = substr(end($RecordedChecksum),0,-4);
				$LiveDirChecksum = getChecksum($WorkDir.DIRECTORY_SEPARATOR.$ImgDir);
				if( $RecordedChecksum === $LiveDirChecksum ){//echo 'Checksum is OK for: '.$ImgDir.'<br/>';//DEBUG
					$Alphas = substr(str_replace($FilePrefix,'',$Item),0,2);
					unset($ProdSeries['Free'][$Alphas]);
					$Numbers = explode('(',$Item);
					$Numbers = explode(')',$Numbers[1]);
					$Numbers = explode('-',$Numbers[0]);
					$ProdSeries['Existing'][$Alphas][$Item]['Min'] = $Numbers[0];
					$ProdSeries['Existing'][$Alphas][$Item]['Max'] = $Numbers[1];
					$ProdSeries['Existing'][$Alphas][$Item]['Total'] = ($Numbers[1]-$Numbers[0])+1;
					$ProdSeries['Existing'][$Alphas][$Item]['Kb'] = round((filesize($WorkDir.DIRECTORY_SEPARATOR.$Item)/1024),1);
					//$Time = explode('|',$Item); $Time = explode('-',$Time[1]);
					//$ProdSeries['Existing'][$Alphas][$Item]['Time'] = strtotime($Time[0].' '.$Time[1]);
					$ProdSeries['Existing'][$Alphas][$Item]['Checksum'] = $RecordedChecksum;
					$ProdSeries['Existing'][$Alphas][$Item]['Dir'] = $WorkDirName.$ImgDir.DIRECTORY_SEPARATOR;
					
					//Get 10 example-files
					$ProdSeries['Existing'][$Alphas][$Item]['Examples'] = array();
					if( $H2=opendir($WorkDir.DIRECTORY_SEPARATOR.$ImgDir) ){
						while( ($Item2=readdir($H2)) !== false ){
							if( count($ProdSeries['Existing'][$Alphas][$Item]['Examples']) > 9 ){
								break 1;
							} else if( strpos($Item2,$ImagePrefix) === 0 && substr($Item2,-4) === '.png' ){
								$ProdSeries['Existing'][$Alphas][$Item]['Examples'][] = $Item2;
							}
						}
						closedir($H2);
					}
					
				} else {//die('Checksum sucks for: '.$ImgDir.'<br/>Zip says: '.$RecordedChecksum.'<br/>Folder says: '.$LiveDirChecksum);//DEBUG
					$Folder1 = $WorkDir.DIRECTORY_SEPARATOR.$ImgDir;
					$Folder2 = str_replace($FilePrefix,'CORRUPT_',$Folder1);
					rename($Folder1,$Folder2);
					$Zip1 = $WorkDir.DIRECTORY_SEPARATOR.$Item;
					$Zip2 = str_replace($FilePrefix,'CORRUPT_',$Zip1);
					rename($Zip1,$Zip2);
				}
			}
		}
		closedir($H);
	} else {
		die('Failed to track Series.');//DEBUG
	}
	
	$ProdSeries['Existing']['Info']['Series'] = count($ProdSeries['Existing']);
	$ProdSeries['Existing']['Info']['Units'] = 0;
	$ProdSeries['Existing']['Info']['Total'] = 0;
	if( $ProdSeries['Existing']['Info']['Series'] > 0 ){
		foreach($ProdSeries['Existing'] as $Alphas => $Units){
			if( $Alphas !== 'Info' ){
				$MinNum = 999999; $MaxNum = 0; $Total = 0;
				foreach( $Units as $Item ){
					$MinNum = ($MinNum>$Item['Min']) ? $Item['Min'] : $MinNum;
					$MaxNum = ($MaxNum<$Item['Max']) ? $Item['Max'] : $MaxNum;
					$Total += $Item['Total'];
				}
				$ProdSeries['Existing'][$Alphas]['Info']['Units'] = count($ProdSeries['Existing'][$Alphas]);
				$ProdSeries['Existing'][$Alphas]['Info']['Min'] = $MinNum;
				$ProdSeries['Existing'][$Alphas]['Info']['Max'] = $MaxNum;
				$ProdSeries['Existing'][$Alphas]['Info']['Total'] = $Total;
				$ProdSeries['Existing']['Info']['Units'] += $ProdSeries['Existing'][$Alphas]['Info']['Units'];
				$ProdSeries['Existing']['Info']['Total'] += $ProdSeries['Existing'][$Alphas]['Info']['Total'];
			}
		}
	}	
	ksort($ProdSeries['Existing']);
	return true;
}



//----------------------------------------------
function getChecksum($ImgDirPath){
	global $ImagePrefix;
	$Checksum = '';
	if( $H=opendir($ImgDirPath) ){
		while( ($Item=readdir($H)) !== false ){
			if( strpos($Item,$ImagePrefix) === 0 && substr($Item,-4) === '.png' ){
				$Checksum .= substr($Item,9,2).substr($Item,12,2).substr(filesize($ImgDirPath.DIRECTORY_SEPARATOR.$Item),-2);
			}
		}
		closedir($H);
	} else {
		return false;
	}
	return hash('adler32',$Checksum);
}

?>